

import org.omg.CORBA.ORB;
import org.omg.CosNaming.NameComponent;
import org.omg.CosNaming.NamingContextExt;
import org.omg.CosNaming.NamingContextExtHelper;
import org.omg.PortableServer.POA;
import org.omg.PortableServer.POAHelper;

import HelloApp.Hello;
import HelloApp.HelloHelper;

public class HelloServer {

	public static void main(String[] args) {
		try {
			//创建一个ORB实例
			ORB orb = ORB.init(args, null);
			//创建一个HelloImpl实例（servant），并注册到ORB上
			HelloImpl helloImpl = new HelloImpl();
			helloImpl.setOrb(orb);
			//得到一个RootPOA的引用，并激活POAManager
			POA rootpoa = POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
			rootpoa.the_POAManager().activate();
			//从服务中得到对象的引用
			org.omg.CORBA.Object ref = rootpoa.servant_to_reference(helloImpl);
			Hello href = HelloHelper.narrow(ref);
			//得到一个根名称的上下文
			org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService");
			NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);
			//在命名上下文中绑定这个对象
			String name = "Hello";
			NameComponent[] path = ncRef.to_name(name);
			ncRef.rebind(path, href);
			System.out.println("HelloServer ready and waiting ...");
			//启动线程服务，等待客户端的调用
			orb.run();
		} catch (Exception e) {
			e.printStackTrace();
		} 
		System.out.println("HelloServer Exiting ...");
	}

}
